<?php
/**
 * Created by 无所谓已
 * BaseController.php
 * Time: 2025/08/21 17:15
 */

namespace app\common;

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use plugin\admin\app\common\Auth;
use plugin\admin\app\common\Util;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;
use support\Model;
use support\Request;
use support\Response;

class BaseController
{
    /**
     * @var Model
     */
    protected $model = null;

    /**
     * 数据限制
     * null 不做限制，任何管理员都可以查看该表的所有数据
     * auth 管理员能看到自己以及自己的子管理员插入的数据
     * personal 管理员只能看到自己插入的数据
     *
     * @var string
     */
    protected $dataLimit = null;


    /**
     * 通用格式化
     *
     * @param $items
     * @param $total
     * @return Response
     */
    protected function formatNormal($items, $total): Response
    {
        return json(['code' => 0, 'msg' => 'ok', 'count' => $total, 'data' => $items]);
    }

    protected function success($msg = '成功', array $data = []): Response
    {
        return $this->json(0, $msg, $data);
    }

    /**
     * 返回格式化json数据
     *
     * @param int    $code
     * @param string $msg
     * @param array  $data
     * @return Response
     */
    protected function json(int $code, string $msg = 'ok', array $data = []): Response
    {
        return json(['code' => $code, 'data' => $data, 'msg' => $msg]);
    }

    protected function fail($msg = '失败', array $data = [], $code = 1): Response
    {
        return $this->json($code, $msg, $data);
    }
}